package ubiqweb.spring3.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ubiqweb.spring3.model.LoginDetails;
import ubiqweb.spring3.model.Training;
import ubiqweb.spring3.service.LoginService;
import ubiqweb.spring3.service.TrainingService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/trainings")
public class TrainingController {

	@Autowired
	private TrainingService trainingService;
	
	@Autowired
	private LoginService loginService;
	
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)	
	public ModelAndView saveTraining(@ModelAttribute(" training") Training training,
			BindingResult result) {
		trainingService.addTraining(training);
		return new ModelAndView("redirect:/trainings.html");
	}
	
	
	@RequestMapping(value = "/topic", method = RequestMethod.GET)
	public ModelAndView listAttendees(HttpSession session, HttpServletRequest request) {
		String trainingId = request.getParameter("trainingId");
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("training",  trainingService.listTrainings(trainingId).get(0));
		return new ModelAndView("showTopic", model);
	}
	
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addTraining(@ModelAttribute("training") Training training,
			BindingResult result) {
		return new ModelAndView("addTraining");
	}
	
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView listTrainings(){
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("trainings", trainingService.listTrainings());
		
		return new ModelAndView("trainingsList", model);
	}


}